<?php
/**
 * Created by PhpStorm.
 * User: Sinri
 * Date: 2017/9/22
 * Time: 21:31
 */

namespace sinri\wenli;


abstract class PageReader
{

    public abstract function version();

    public function readPage($url)
    {
        $begin = microtime(true);
        $html = file_get_contents($url);
        $end = microtime(true);

        echo __METHOD__ . " spent " . ($end - $begin) . " seconds for " . $url . PHP_EOL;
        return $html;
    }

    public function bookChapterMapping($bookId = -1)
    {
        $map = [
            //舊約
            50, 40, 27, 36, 34, 24, 21, 4, 31, 24, 22, 25, 29, 36, 10, 13, 10, 42, 150, 31, 12, 8, 66, 52, 5, 48, 12, 14, 3, 9, 1, 4, 7, 3, 3, 3, 2, 14, 4,
            //新約
            28, 16, 24, 21, 28, 16, 16, 13, 6, 6, 4, 4, 5, 3, 6, 4, 3, 1, 13, 5, 5, 3, 5, 1, 1, 1, 22,
        ];
        if ($map < 0) {
            return $map;
        }
        return $map[$bookId];
    }

    public function dutyForBook($book_id, $book_name, $chapter_count)
    {
        $html_dir = __DIR__ . '/../../../html/' . $this->version() . '/';
        @mkdir($html_dir, 0777, true);
        $filename = $html_dir . $book_id . "-" . $book_name . "-" . $chapter_count . ".htm";

        if (!file_exists($filename)) {
            $url = $this->getBookURL($book_id, $book_name, $chapter_count);
            $html = $this->readPage($url);

            //save
            file_put_contents($filename, $html);
        } else {
            $html = file_get_contents($filename);
        }

        $chapters = $this->html2txt($html);

        foreach ($chapters as $chapter_code => $content) {
            $target_txt_dir = __DIR__ . '/../../../txt/' . $this->version() . '/' . $book_id . "-" . $book_name . "-" . $chapter_count;
            @mkdir($target_txt_dir, 0777, true);
            $target_txt_file = $target_txt_dir . '/第' . $chapter_code . '章.txt';
            file_put_contents($target_txt_file, $content);
            echo "DONE - " . $target_txt_file . PHP_EOL;
        }
    }

    protected abstract function getBookURL($book_id, $book_name = null, $chapter_id = null);

    protected abstract function html2txt($html, $chapter = null);

    protected function bookIdToReadIndex($book_id)
    {
        return ($book_id < 10 ? '0' : '') . $book_id;
    }

    public function dutyForBookByEachChapter($book_id, $book_name, $chapter_count)
    {
        echo __METHOD__ . ' book id: ' . $book_id . ' book name: ' . $book_name . ' chap: ' . $chapter_count . PHP_EOL;
        for ($chapter_id = 1; $chapter_id <= $chapter_count; $chapter_id++) {
            $html_dir = __DIR__ . '/../../../html/' . $this->version() . '/' . $book_name . "/";
            @mkdir($html_dir, 0777, true);
            $filename = $html_dir . $book_id . "-" . $book_name . "-" . $chapter_id . ".htm";

            if (!file_exists($filename)) {
                $url = $this->getBookURL($book_id, null, $chapter_id);
                $html = $this->readPage($url);

                if (empty($html)) {
                    echo "ERROR: EMPTY CONTENT from " . $url . PHP_EOL;
                    continue;
                }

                //save
                file_put_contents($filename, $html);
            } else {
                $html = file_get_contents($filename);
            }

            $content = $this->html2txt($html, $chapter_id);

            $txt_book_id = $this->bookIdToReadIndex($book_id);

            $target_txt_dir = __DIR__ . '/../../../txt/' . $this->version() . '/' . $txt_book_id . "-" . $book_name . "-" . $chapter_count . "章";
            @mkdir($target_txt_dir, 0777, true);
//            $target_txt_file = $target_txt_dir . '/第' . $chapter_id . '章.txt';
            $target_txt_file = $target_txt_dir . '/' . ($chapter_id < 100 ? '0' : '') . ($chapter_id < 10 ? '0' : '') . $chapter_id . '.txt';
            file_put_contents($target_txt_file, $content);
            echo "DONE - " . $target_txt_file . PHP_EOL;
        }
    }
}